package com.fp.rule.starter;

import com.sfjswl.fp.core.utils.RuleExecutor;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.*;

@Component
public class RuleInit implements InitializingBean {

  @Resource
  private RuleProperties properties;

  @Override
  public void afterPropertiesSet() throws Exception {
    ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
      properties.getCorePoolSize(),
      properties.getMaximumPoolSize(),
      3,
      TimeUnit.SECONDS,
      new LinkedBlockingDeque<>(properties.getMaximumPoolSize()),
      Executors.defaultThreadFactory(),
      new ThreadPoolExecutor.CallerRunsPolicy());

    RuleExecutor.setExecutor(threadPoolExecutor);
  }
}